% This file analyzes the experiments intended to show that g is phi
% independent
load('./data/FigS13.mat')
%% process experiment data
r=2e6;E_pert=0.1;

alpha_sweep = pi/8*ones(1,2);
phi_sweep=0:pi/3:2*pi;

%% readout data for g_beta,phi
flag = {[0,1,1],[0,1,-1]};
rabi_estimate_save_bp=zeros(2,2,7,2); % 1: SQ, 2: DQ

for hlp_alpha=1:2
    alpha=alpha_sweep(hlp_alpha);
    for hlp_phi=1:length(phi_sweep) % scan beta or phi
        for hlp_flg=1:length(flag)
            flg_tmp=flag{hlp_flg};
            alpha_oscillate=flg_tmp(1);
            beta_oscillate=flg_tmp(2);
            phi_oscillate=flg_tmp(3);
            
            for hlp_omega=1:2 % omega
                
                switch hlp_omega
                    case 1
                        d_alphaH=-1;
                        d_betaH=1i*sin(2*alpha)/2;
                        d_phiH=-1i*sin(2*alpha)/2;
                    case 2
                        d_alphaH=0;
                        d_betaH=-1i*cos(alpha)^2;
                        d_phiH=-1i*sin(alpha)^2;
                end
                
                rabi_estimate=E_pert*abs(d_alphaH*alpha_oscillate+d_betaH*beta_oscillate+d_phiH*phi_oscillate);
                
                if hlp_omega==1
                    rabi_estimate=rabi_estimate/sqrt(2);
                end
                
                rabi_estimate=rabi_estimate*r;
                rabi_estimate_save_bp(hlp_omega,hlp_flg,hlp_phi,hlp_alpha)=rabi_estimate;
            end
        end
    end
end

%% now repeat the same for g_alpha,alpha
flag={[1,0,0]};

rabi_estimate_save_aa=zeros(2,2,7,2); % 1: SQ, 2: DQ

beta=0;phi=0;

for hlp_alpha=1:2
    alpha=alpha_sweep(hlp_alpha);
    for hlp_phi=1:length(phi_sweep) % scan beta or phi
        for hlp_flg=1:length(flag)
            flg_tmp=flag{hlp_flg};
            alpha_oscillate=flg_tmp(1);
            beta_oscillate=flg_tmp(2);
            phi_oscillate=flg_tmp(3);
            
            for hlp_omega=1:2 % omega
               
                
                switch hlp_omega
                    case 1
                        d_alphaH=-1;
                        d_betaH=1i*sin(2*alpha)/2;
                        d_phiH=-1i*sin(2*alpha)/2;
                    case 2
                        d_alphaH=0;
                        d_betaH=-1i*cos(alpha)^2;
                        d_phiH=-1i*sin(alpha)^2;
                end
                
                rabi_estimate=E_pert*abs(d_alphaH*alpha_oscillate+d_betaH*beta_oscillate+d_phiH*phi_oscillate);
                
                if hlp_omega==1
                    rabi_estimate=rabi_estimate/sqrt(2);
                end
                
                rabi_estimate=rabi_estimate*r;
                rabi_estimate_save_aa(hlp_omega,hlp_flg,hlp_phi,hlp_alpha)=rabi_estimate;
            end
        end
    end
end

%% plot Gamma

xlabel_name={'\beta(\circ)','\phi(\circ)'};
for hlp_alpha=1:2
    figure
    cc=colormap(piratepal(9,1));
    subplot(1,2,1) % SQ
    hold on
    
    
    for hlp=1:2
        plot(phi_sweep/pi*180,reshape(rabi_estimate_save_bp(1,hlp,:,hlp_alpha),1,[])./reshape(E_pert_save_bp(1,hlp,:,hlp_alpha),1,[])/1e6,'LineWidth',1.5,'Color',cc(hlp,:),'HandleVisibility','off');
        errorbar(phi_sweep/pi*180,reshape(rabi_fit_save_bp(1,hlp,:,hlp_alpha),1,[])./reshape(E_pert_save_bp(1,hlp,:,hlp_alpha),1,[])/1e6,reshape(drabi_fit_save_bp(1,hlp,:,hlp_alpha),1,[])./reshape(E_pert_save_bp(1,hlp,:,hlp_alpha),1,[])/1e6,'o','MarkerSize',12,'LineWidth',1.5,'Color',cc(hlp,:));
    end
    plot(phi_sweep/pi*180,reshape(rabi_estimate_save_aa(1,1,:,hlp_alpha),1,[])./reshape(E_pert_save_aa(1,1,:,hlp_alpha),1,[])/1e6,'LineWidth',1.5,'Color',cc(3,:),'HandleVisibility','off');
    errorbar(phi_sweep/pi*180,reshape(rabi_fit_save_aa(1,1,:,hlp_alpha),1,[])./reshape(E_pert_save_aa(1,1,:,hlp_alpha),1,[])/1e6,reshape(drabi_fit_save_aa(1,1,:,hlp_alpha),1,[])./reshape(E_pert_save_aa(1,1,:,hlp_alpha),1,[])/1e6,'o','MarkerSize',12,'LineWidth',1.5,'Color',cc(3,:));
    
    xlim([0,360])
    xticks(0:90:360)
    ylim([0,1.6])
    xlabel(xlabel_name{hlp_alpha},'FontSize',18)
    ylabel('\Gamma (MHz)','FontSize',18)
    title('SQ','FontSize',20)
    hold off
    set(gca,'FontSize',18)
    
    subplot(1,2,2) % DQ
    hold on
    
    for hlp=1:2
        plot(phi_sweep/pi*180,reshape(rabi_estimate_save_bp(2,hlp,:,hlp_alpha),1,[])./reshape(E_pert_save_bp(2,hlp,:,hlp_alpha),1,[])/1e6,'LineWidth',1.5,'Color',cc(hlp,:),'HandleVisibility','off');
        errorbar(phi_sweep/pi*180,reshape(rabi_fit_save_bp(2,hlp,:,hlp_alpha),1,[])./reshape(E_pert_save_bp(2,hlp,:,hlp_alpha),1,[])/1e6,reshape(drabi_fit_save_bp(2,hlp,:,hlp_alpha),1,[])./reshape(E_pert_save_bp(2,hlp,:,hlp_alpha),1,[])/1e6,'o','MarkerSize',12,'LineWidth',1.5,'Color',cc(hlp,:));
    end
    plot(phi_sweep/pi*180,reshape(rabi_estimate_save_aa(2,1,:,hlp_alpha),1,[])./reshape(E_pert_save_aa(2,1,:,hlp_alpha),1,[])/1e6,'LineWidth',1.5,'Color',cc(3,:),'HandleVisibility','off');
    errorbar(phi_sweep/pi*180,reshape(rabi_fit_save_aa(2,1,:,hlp_alpha),1,[])./reshape(E_pert_save_aa(2,1,:,hlp_alpha),1,[])/1e6,reshape(drabi_fit_save_aa(2,1,:,hlp_alpha),1,[])./reshape(E_pert_save_aa(2,1,:,hlp_alpha),1,[])/1e6,'o','MarkerSize',12,'LineWidth',1.5,'Color',cc(3,:));
    
    xlim([0,360])
    xticks(0:90:360)
    ylim([0,2.2])
    title('DQ','FontSize',20)
    hold off
    set(gca,'FontSize',18)
    lgd=legend({'$\Gamma^{\beta\phi}$','$\Gamma^{\beta\bar{\phi}}$','$\Gamma^{\alpha}$'});
    set(lgd,'Location','best','FontSize',18,'Interpreter','Latex')
    
%     saveas(gcf,['FigS13_',num2str(hlp_alpha)],'epsc')
end

%% plot g components
for hlp=1:2
    figure
    hold on
    plot(phi_sweep/pi*180,1/32*(cos(4*alpha_sweep(hlp))-1)*ones(size(phi_sweep)),'LineWidth',1.5,'HandleVisibility','off','Color',cc(1,:))
    errorbar(phi_sweep/pi*180,g_munu_save_bp(:,hlp),g_munu_err_save_bp(:,hlp),'d','MarkerSize',12,'LineWidth',1.5,'Color',cc(1,:))
    plot(phi_sweep/pi*180,1/2*ones(size(phi_sweep)),'LineWidth',1.5,'HandleVisibility','off','Color',cc(2,:))
    errorbar(phi_sweep/pi*180,g_munu_save_aa(:,hlp),g_munu_err_save_aa(:,hlp),'d','MarkerSize',12,'LineWidth',1.5,'Color',cc(2,:))
    
    xlim([0,360])
    xticks(0:90:360)
    ylim([-0.15,0.65])
    xlabel(xlabel_name{hlp},'FontSize',18)
    ylabel('g_{\mu\nu}','FontSize',18)
    legend({'g_{\beta\phi}','g_{\alpha\alpha}'},'FontSize',18,'Location','east')
    set(gca,'FontSize',18)
    
%     saveas(gcf,['FigS13_',num2str(hlp+2)],'epsc')
end







